package yz.cp.back.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import yz.cp.back.admin.service.MessageSendManage;
import yz.cp.back.common.base.BaseController;
import yz.cp.back.common.res.ApiResult;
import yz.cp.back.s.entity.SysMessage;
import yz.cp.back.s.entity.SysMessageSend;
import yz.cp.back.s.entity.SysUser;
import yz.cp.back.s.service.ISysMessageService;
import yz.cp.back.s.service.ISysUserService;


/**
 * 消息发送管理接口
 * @author xukk
 */
@Slf4j
@RestController
@RequestMapping("/web/messageSend")
public class MessageSendController extends BaseController {

    @Autowired
    private MessageSendManage messageSendManage;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysMessageService sysMessageService;
    /**
     * 多条件分页获取
     * @param ms
     * @param pv
     * @return
     */
    @RequestMapping(value = "/getByCondition",method = RequestMethod.GET)
    public ApiResult getByCondition(@ModelAttribute SysMessageSend ms,@PageableDefault Pageable pageable){

        IPage<SysMessageSend> page = messageSendManage.findByCondition(ms, pageable);
        // lambda
        page.convert(item->{
            SysUser u = sysUserService.getById(item.getUserId());
            if(u!=null){
                item.setUsername(u.getUsername());
            }
            SysMessage m = sysMessageService.getById(item.getMessageId());
            if(m!=null){
                item.setTitle(m.getTitle());
                item.setContent(m.getContent());
                item.setType(m.getType());
            }
            return item;
        });
        return renderOk().add(page);
    }
    @RequestMapping(value = "/count",method = RequestMethod.GET)
    public ApiResult count(@ModelAttribute SysMessageSend ms) {
        return renderOk().add(messageSendManage.count(ms));
    }
}
